package client.comm.baoding.ui

import android.view.View
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.Td2Adapter
import client.comm.baoding.api.bean.TdBean
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.ActivityTeam2Binding
import client.comm.baoding.ui.vm.MainViewModel
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.commlib.widget.EmptyLayout
import com.blankj.utilcode.util.LogUtils

/**
 * Date: 2022/11/24 10:37
 * Description: 团队页面
 */
class TeamActivity : BaseMiddleActivity<MainViewModel, ActivityTeam2Binding>() {

    override fun getLayout(): Int {
        return R.layout.activity_team2
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    val adapter: Td2Adapter by lazy {
        Td2Adapter(this, mImgLoader)
    }

    override fun initView() {
        super.initView()
        binding.event = this
        binding.recyclerView.adapter = adapter
//        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))
        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true
            viewModel.pageOne.value = 0
            viewModel.get_user_team()
        }
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.get_user_team()
            }
        }
        binding.emptyLayout.setOnClickListener {
            adapter.hasMoreState.value = true
            viewModel.pageOne.value = 0
            viewModel.get_user_team()
        }
    }


    override fun initData() {
        super.initData()

        var userId = StoreUtil.getBean<UserInfo>(this)!!.id
        if (userId.isNullOrEmpty()) {
            toast("用户id不能为空")
            return
        }
        LogUtils.i("userId= $userId")
        viewModel.userId.value = userId

        adapter.hasMoreState.value = true
        viewModel.pageOne.value = 0
        viewModel.get_user_team()

        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list = it.data?.sub_list as MutableList<TdBean.Sub?>
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitData(list, it.data!!)
//                        binding.title = "${it.data?.nickname}的团队"
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }


}